#include "utimer_hal.h"
#define PWM_TEST
#ifdef PWM_TEST 
void test_pwm_set_50(void)
{
	utimer0_set_pwm(50);
}
#endif 


/**
 * @brief 定时器0 初始化，用于系统时钟 系统96M/2  48M
 * 
 */
void utimer1_hal_init(void)
{
    TIM_TimerInitTypeDef TIM_InitStruct;

    TIM_TimerCmd(TIMER1, ENABLE); /* Timer0 模块使能 */

    TIM_TimerStrutInit(&TIM_InitStruct);                  //Timer结构体初始化
    TIM_InitStruct.Timer_CH0_WorkMode = TIMER_OPMode_CMP; /* 设置Timer CH0 为比较模式 */
    TIM_InitStruct.Timer_CH0_CapMode = TIMER_CapMode_None;
    TIM_InitStruct.Timer_CH0Output = 0;                   /* 计数器回零时，比较模式输出极性控制 */
    TIM_InitStruct.Timer_CH1_WorkMode = TIMER_OPMode_CMP; /* 设置Timer CH1 为比较模式 */
    TIM_InitStruct.Timer_CH1_CapMode = TIMER_CapMode_None;
    TIM_InitStruct.Timer_CH1Output = 0; /* 计数器回零时，比较模式输出极性控制 */
    TIM_InitStruct.Timer_TH = 48000;    /* 定时器计数门限初始值48000*/
    TIM_InitStruct.Timer_CMP0 = 24000;  /* 设置比较模式的CH0比较初始值24000 */
    TIM_InitStruct.Timer_CMP1 = 24000;  /* 设置比较模式的CH1比较初始值24000 */
    TIM_InitStruct.Timer_Filter0 = 0;   /* 设置捕捉模式或编码器模式下对应通道的数字滤波值 */
    TIM_InitStruct.Timer_Filter1 = 0;   /* 设置捕捉模式或编码器模式下对应通道的数字滤波值 */
    TIM_InitStruct.Timer_ClockDiv = TIM_Clk_Div2;                                          /* 设置Timer模块数据2分频系数 */
    TIM_InitStruct.Timer_IRQEna = Timer_IRQEna_CH0; /* 开启Timer模块比较中断和过零中断*/
    TIM_TimerInit(TIMER1, &TIM_InitStruct);


}
void timer1_hal_enable(void)
{
	 __disable_irq();
    NVIC_SetPriority(TIMER1_IRQn, 4); //TIMER0中断优先级配置
    NVIC_EnableIRQ(TIMER1_IRQn);      //使能UTimer定时器中断
    __enable_irq(); /* 开启中断 */
}
/**
 * @brief Get the clock val object
 * 
 * @return uint32_t 当前系统时钟
 */
uint32_t get_clock_val(void)
{
    return jiffies;
}

static void utimer0_pwm_gpio_config(void)
{
		GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_StructInit(&GPIO_InitStruct); //初始化结构体
    /* 配置UTimer0  TIM0_CH0: P1.10*/
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
    GPIO_Init(GPIO1, &GPIO_InitStruct);
		GPIO_PinAFConfig(GPIO1, GPIO_PinSource_10, AF7_TIMER01);
		
}
/**
 * @brief pwm 定时器初始化
 * 
 */
void utimer0_pwm_init(void)
{
   TIM_TimerInitTypeDef TIM_InitStruct;

		utimer0_pwm_gpio_config();
	 
    TIM_TimerCmd(TIMER0, ENABLE); /* Timer0 模块使能 */
    TIM_TimerStrutInit(&TIM_InitStruct);                  //Timer结构体初始化
    TIM_InitStruct.Timer_CH0_WorkMode = TIMER_OPMode_CMP; /* 设置Timer CH1 为比较模式 */
    TIM_InitStruct.Timer_CH0_CapMode = TIMER_CapMode_None;
    TIM_InitStruct.Timer_CH0Output = 1; 										/* 计数器回零时，比较模式输出极性控制 */
    TIM_InitStruct.Timer_TH = 50000+1;    										/* 定时器计数门限初始值1000*/
    TIM_InitStruct.Timer_CMP0 = 10000;  										/* 设置比较模式的CH1比较初始值24000 */
    TIM_InitStruct.Timer_Filter0 = 0;  					 /* 设置捕捉模式或编码器模式下对应通道的数字滤波值 */
    TIM_InitStruct.Timer_Filter1 = 0;   /* 设置捕捉模式或编码器模式下对应通道的数字滤波值 */
    TIM_InitStruct.Timer_ClockDiv = TIM_Clk_Div2;                                          /* 设置Timer模块数据2分频系数 */
    TIM_InitStruct.Timer_IRQEna = Timer_IRQEna_CH0; /* 开启Timer模块比较中断和过零中断*/
    TIM_TimerInit(TIMER0, &TIM_InitStruct);
    __disable_irq();
    NVIC_SetPriority(TIMER0_IRQn, 3); //TIMER3中断优先级配置
    NVIC_EnableIRQ(TIMER0_IRQn);      //使能UTimer定时器中断
    __enable_irq(); /* 开启中断 */
#ifdef PWM_TEST 
		test_pwm_set_50();
#endif
}
/**
 * @brief 设置pwm
 * 
 * @param pre_val 0-100 对应占空比0~100% 
 */
void utimer0_set_pwm(uint8_t pre_val)
{
	uint16_t val=10;
	if(pre_val>=100)pre_val=99;
	if(pre_val<1) pre_val = 1;
	val = pre_val*500;
	TIMER0->CMPT0 = val;  
}

